package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpFaUserFeedbackDTO;
import com.financing.platform.service.IFpFaUserFeedbackService;
import com.financing.platform.util.DTOdateSet;

@Controller("fpFaUserFeedbackController")
@RequestMapping("/api/fpFaUserFeedback")
public class FpFaUserFeedbackController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpFaUserFeedbackService fpFaUserFeedbackService;
	
	@RequestMapping("/listFpFaUserFeedback.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUserFeedback list begin .......");
			
			fpFaUserFeedback.setBegin(pager.getStartIndex());
			fpFaUserFeedback.setPageSize(pager.getPageSize());
			
			int count = fpFaUserFeedbackService.queryListCount(fpFaUserFeedback);
			
			pager.setTotalCount(count);
			
			List<FpFaUserFeedbackDTO> fpFaUserFeedbackList = null;
			
			if(count > 0){
				fpFaUserFeedbackList = fpFaUserFeedbackService.queryList(fpFaUserFeedback);
				for (int i = 0; i < fpFaUserFeedbackList.size(); i++) {
					FpFaUserFeedbackDTO ffuf = new FpFaUserFeedbackDTO(); 
					ffuf.setFpUserPhone(fpFaUserFeedbackList.get(i).getFpUserPhone());
					List<FpFaUserFeedbackDTO> flists =fpFaUserFeedbackService.queryList(ffuf);
					fpFaUserFeedbackList.get(i).setBackList(flists);
				}
			}else{
				fpFaUserFeedbackList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpFaUserFeedbackList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUserFeedback list end .......");
		}catch(Exception e){
			log.error("query fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpFaUserFeedback.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaUserFeedback  begin .......");
			
			DTOdateSet.setUpdateInfo(fpFaUserFeedback, request.getSession());
			DTOdateSet.setCreateInfo(fpFaUserFeedback, request.getSession());
			
			int id = fpFaUserFeedbackService.insert(fpFaUserFeedback);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaUserFeedback list end .......");
		}catch(Exception e){
			log.error("add fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpFaUserFeedback.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpFaUserFeedbackDTO dto = fpFaUserFeedbackService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpFaUserFeedback  end .......");
		}catch(Exception e){
			log.error("query detail fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpFaUserFeedback")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpFaUserFeedbackService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpFaUserFeedback  end .......");
		}catch(Exception e){
			log.error("delete fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpFaUserFeedback")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(fpFaUserFeedback, request.getSession());
			
			fpFaUserFeedbackService.update(fpFaUserFeedback);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpFaUserFeedback list end .......");
		}catch(Exception e){
			log.error("update fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpFaUserFeedback")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(fpFaUserFeedback, request.getSession());
			
			fpFaUserFeedbackService.updateForce(fpFaUserFeedback);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpFaUserFeedback list end .......");
		}catch(Exception e){
			log.error("updateForce fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
